package com.example.springbootmvc;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-10
 * Time: 20:41
 */
@Controller//返回视图
public class IndexController {
    //返回一个静态页面
    @RequestMapping("/index")
    public Object index(){
        return "/index.html";
    }
    @RequestMapping("/index2")
    @ResponseBody
    public Object index2(){
        return "/index.html";
    }

    @RequestMapping("/returnHtml1")
    @ResponseBody
    public String returnHtml1(){
        return "<h1>Hello HTML!!!</h1>";
    }
    @RequestMapping("/returnHtml")
    @ResponseBody
    public String returnHtml() {
        return"<h1>Hello,HTML~</h1>";
    }

    @RequestMapping("/returnJson")
    @ResponseBody
    public HashMap<String,String> returnJson(){
        HashMap<String,String> hashmap = new HashMap<>();
        hashmap.put("张三","畏罪潜逃");
        hashmap.put("李四","知法犯法");
        hashmap.put("王五","不知悔改");
        return hashmap;
    }

    @RequestMapping(value = "/setStatus")
    @ResponseBody
    public String setStatus(HttpServletResponse response){
        response.setStatus(401);
        return "设置状态码成功";
    }

    @RequestMapping(value = "/returnJson2" , produces = "application/json")
    @ResponseBody
    public String returnJson2(){
        return "{\"success\":true}";//转义字符
    }


    @RequestMapping(value = "/returnJson3" ,
            produces = "application/json;charset = utf-8")
    @ResponseBody
    public String returnJson3(){
        return "{\"你大爷\":true}";//转义字符
    }

    @RequestMapping("/setHeader")
    @ResponseBody
    public String setHeader(HttpServletResponse response){
        response.setHeader("柳贯一","boss");
        return "设置header成功";
    }
    @RequestMapping("/setHeader2")
    @ResponseBody
    public String setHeader2(HttpServletResponse response){
        response.setHeader("myheader","value");
        return "设置header成功";

    }




}

